package com.datastructure.linkedlist.self;

public class YuesefuQuestion {


}

class CircleLinkedList{

    public Carriage first = null;

    public void addCarriage(int nums){
        if(nums<=0){
           System.out.println("请输入有效的数量");
        }
        Carriage helper=null;
        Carriage carriage;
        for(int i=0;i<nums;i++){
            carriage = new Carriage(i);
            if(i==0){
                first = carriage;
                first.setNext(first);
                helper=first;
            } else {
                helper.setNext(carriage);
                carriage.next=first;
                helper=carriage;
            }
        }
    }

    public void showCarriage(){
        if(first==null){
            System.out.printf("链表中没有数据");
            return;
        }
        Carriage carriage=first;
        while (true){
            if(carriage.next==first){
                break;
            }
            System.out.printf("这次输入的结果是%d\n",carriage.no);
            carriage = carriage.next;
        }
    }

    public void countCarriage(int startNo, int nums, int total){
        if(startNo>total||nums>total){
            System.out.printf("请输入正确的数值:");
            return;
        }
        Carriage helper = first;
        while (true){
            if(helper.next==first){
                break;
            }
            helper=helper.next;
        }
        for(int i=0;i<startNo-1;i++){
            first = first.next;
            helper = helper.next;
        }
        while (true){
            if(helper==first){
                break;
            }
            for(int j=0;j<nums-1;j++){
                first=first.next;
                helper=helper.next;
            }
            System.out.printf("取出的车厢号码是%d\n",first.no);
            first=first.next;
            helper.next=first;
        }
    }
}

class Carriage {
    public int no;
    public Carriage next;

    public Carriage(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Carriage getNext() {
        return next;
    }

    public void setNext(Carriage next) {
        this.next = next;
    }
}
